package com.smyh.UIM;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

import com.smyh.adapter.AddrItemAdapter;
import com.smyh.bean.DeviceInfo;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements OnClickListener {
    private static final String TAG = "MainActivity";

    private LinearLayout leftMenu;
    private LinearLayout content;
    ListView list_view;

    private LayoutParams leftMenuParams;
    private LayoutParams contentParams;

    // menu完全显示时，留给content的宽度值。
    private static final int menuPadding = 300;
    private ImageView leftBtn;

    // 分辨率
    private int disPlayWidth;

    private boolean mIsShow = false;

    private static final int speed = 100;
    public boolean isMenu = false;

    List<DeviceInfo> deviceInfos;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }
    private void initData() {
        deviceInfos = new ArrayList<>();
        DeviceInfo info = new DeviceInfo();
        info.setConnection(true);
        info.setOnline(false);
        info.setDeviceAddr("00 00 00 01");
        info.setDeviceType("64");
        deviceInfos.add(info);
        DeviceInfo info1 = new DeviceInfo();
        info1.setConnection(false);
        info1.setOnline(true);
        info1.setDeviceAddr("00 00 00 02");
        info1.setDeviceType("16");
        deviceInfos.add(info1);

    }

    /**
     * 初始化视图 TODO
     */
    private void initView() {
        disPlayWidth = getWindowManager().getDefaultDisplay().getWidth();
        leftMenu = (LinearLayout) findViewById(R.id.leftMenu);
        content = (LinearLayout) findViewById(R.id.content);
        list_view = (ListView) findViewById(R.id.list_view);

        list_view.setAdapter(new AddrItemAdapter(MainActivity.this,R.layout.addr_item,deviceInfos));

        leftMenuParams = (LayoutParams) leftMenu.getLayoutParams();
        contentParams = (LayoutParams) content.getLayoutParams();

        leftMenuParams.width = disPlayWidth - menuPadding;
        contentParams.width = disPlayWidth;

        leftBtn = (ImageView) findViewById(R.id.leftBtn);

        leftBtn.setOnClickListener(this);

        showMenu(mIsShow);
    }

    /**
     * 显示界面 TODO
     */
    private void showMenu(boolean isShow) {
        if (isShow) {
            mIsShow = true;
            leftMenuParams.leftMargin = 0;
        } else {
            mIsShow = false;
            leftMenuParams.leftMargin = 0 - leftMenuParams.width;
        }
        leftMenu.setLayoutParams(leftMenuParams);
    }

    /**
     * TODO 控制左侧View
     */
    class showMenuAsyncTask extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected Integer doInBackground(Integer... params) {
            int leftMargin = leftMenuParams.leftMargin;
            Log.i(TAG, "leftMargin=" + leftMargin);
            while (true) {
                leftMargin += params[0];
                Log.i(TAG, "doInBackground:leftMargin" + leftMargin);
                if (params[0] >= 0 && leftMargin >= 0) {
                    leftMargin = 0;
                    break;
                } else if (params[0] < 0 && leftMargin <= -leftMenuParams.width) {
                    leftMargin =  -leftMenuParams.width;
                    break;
                }
                publishProgress(leftMargin);
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return leftMargin;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            leftMenuParams.leftMargin = values[0];
            leftMenu.setLayoutParams(leftMenuParams);
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            leftMenuParams.leftMargin = result;
            leftMenu.setLayoutParams(leftMenuParams);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.leftBtn:

                if (isMenu) {
                    isMenu = false;
                    new showMenuAsyncTask().execute(-speed);
                } else {
                    isMenu = true;
                    new showMenuAsyncTask().execute(speed);
                }

                break;
            case R.id.content:

                break;
        }
    }

}
